import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Admin from '../views/Admin/Index.vue'
import Role from '../views/Role/Index.vue'
import Rule from '../views/Rule/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/admin',
        name: 'Admin',
        component: Admin,
      },
      {
        path: '/role',
        name: 'Role',
        component: Role,
      },
      {
        path: '/rule',
        name: 'Rule',
        component: Rule,
      },
    ],

  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next)=>{
  if (to.path == '/login') {
    next()
  }
  if (!window.sessionStorage.getItem('token') || window.sessionStorage.getItem('token') == 'null' || window.sessionStorage.getItem('token') == '' || window.sessionStorage.getItem('token') == undefined) {
    next('/login')
  }
  next()
});

export default router
